using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.SqlServer.Dts.Runtime.Design;
using Microsoft.SqlServer.Dts.Runtime;
using System.Collections;
using Shutterstock.Salesforce.SSISDownload_UI;
using Shutterstock.Salesforce.SSISDownload;
using Shutterstock.Salesforce.SSISDownload.SSISTask;

namespace Shutterstock.Salesforce.SSISDownload_UI
{
    public partial class SFRefreshTaskUIForm : Form
    {
        TaskHost _taskHost;
        IDtsConnectionService _connectionService;

        public SFRefreshTaskUIForm(TaskHost taskHost, IDtsConnectionService connectionService)
        {
            InitializeComponent();
            _taskHost = taskHost;
            _connectionService = connectionService;
            Connections connections = connectionService.GetConnections();
            foreach (ConnectionManager cm in connections)
            {
                this.comboBox1.Items.Add(cm.Name);
            }
            this.comboBox2.Items.Add(ReportingLevel.Low);
            this.comboBox2.Items.Add(ReportingLevel.High);

            
            this.bulkCutoffBox.Text = this._taskHost.Properties[Constants.BULK_CUTOFF].GetValue(_taskHost).ToString();
            this.BulkApiBoolean.Checked = Boolean.Parse(this._taskHost.Properties[Constants.USE_BULK_API].GetValue(_taskHost).ToString());
            this.txtBoxUserName.Text = this._taskHost.Properties[Constants.PERSIST_USERNAME].GetValue(_taskHost).ToString();
            this.mtxtBoxPassword.Text = this._taskHost.Properties[Constants.PRESIST_PASSWORD].GetValue(_taskHost).ToString();
            this.numericUpDown1.Value = Convert.ToInt32(this._taskHost.Properties[Constants.PERSIST_MAXTHREAD].GetValue(_taskHost).ToString());
            this.numericUpDown2.Value= Convert.ToInt32(this._taskHost.Properties[Constants.PERSIST_NUMRETRIES].GetValue(_taskHost).ToString());
            this.txtBoxBatchSize.Text = this._taskHost.Properties[Constants.PERSIST_BATCHSIZE].GetValue(_taskHost).ToString();
            this.chkBoxManualComparison.Checked = Convert.ToBoolean(this._taskHost.Properties[Constants.PERSIST_MANUALCOMPARISON].GetValue(taskHost).ToString());
            this.comboBox1.SelectedItem = this._taskHost.Properties[Constants.PERSIST_SQLDESTCONMGR].GetValue(_taskHost).ToString();
            this.comboBox2.SelectedItem = (ReportingLevel)Enum.Parse(typeof(ReportingLevel),
            this._taskHost.Properties[Constants.PERSIST_MESSAGELEVEL].GetValue(_taskHost).ToString());

            bool allow = Convert.ToBoolean(this._taskHost.Properties[Constants.PERSIST_ALLOW_EXCEPTIONS].GetValue(_taskHost).ToString());
            if (allow)
            {
                bool defaultStatus = Convert.ToBoolean(this._taskHost.Properties[Constants.PERSIST_DEFAULT_STATUS].GetValue(_taskHost).ToString());
                if (defaultStatus)
                {
                    this.DefaultStatusBox.SelectedIndex = 0;
                }
                else
                {
                    this.DefaultStatusBox.SelectedIndex = 1;
                }

                this.AllowExceptions.Checked = allow;
                this.tableNameBox.Text = this._taskHost.Properties[Constants.PERSIST_EXCEPTION_TABLE_NAME].GetValue(_taskHost).ToString();
                this.ColumnNameBox.Text = this._taskHost.Properties[Constants.PERSIST_EXCEPTION_COLUMN_NAME].GetValue(_taskHost).ToString();
                this.forceBulkBox.Text = this._taskHost.Properties[Constants.PERSIST_EXCEPTION_FORCE_BULK_COLUMN_NAME].GetValue(_taskHost).ToString();
                this.ForceStandardBox.Text = this._taskHost.Properties[Constants.PERSIST_EXCEPTION_FORCE_STANDARD_COLUMN_NAME].GetValue(_taskHost).ToString();
                this.objectNameBox.Text = this._taskHost.Properties[Constants.PERSIST_EXCEPTION_OBJECT_COLUMN_NAME].GetValue(_taskHost).ToString();
            }

            bool dmlEmailsUsed = Convert.ToBoolean(this._taskHost.Properties[Constants.PERSIST_SEND_DML_EMAIL].GetValue(_taskHost).ToString());
            if (dmlEmailsUsed)
            {
                this.sendDmlCheckBox.Checked = true;
                this.fromTxtbox.Text = this._taskHost.Properties[Constants.PERSIST_DML_FROM_ADDRESS].GetValue(_taskHost).ToString();
                this.toTxtbox.Text = this._taskHost.Properties[Constants.PERSIST_DML_TO_ADDRESS].GetValue(_taskHost).ToString();
                this.smtpTxtbox.Text = this._taskHost.Properties[Constants.PERSIST_DML_SMTP_ADDRESS].GetValue(_taskHost).ToString();
            }

        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            int batchSize;
            int cutoff;
            bool ok = int.TryParse(this.txtBoxBatchSize.Text, out batchSize);
            bool cutoffOk = int.TryParse(this.bulkCutoffBox.Text, out cutoff);

            bool allow = this.AllowExceptions.Checked;
            bool sendDml = this.sendDmlCheckBox.Checked;
            if (!ok || batchSize < 0)
            {
                this.errorProvider1.SetError(this.txtBoxBatchSize, "Please enter 0 or a positive integer for batch size");
                this.DialogResult = DialogResult.None;
            }
            else if (!cutoffOk)
            {
                this.errorProvider1.SetError(this.bulkCutoffBox, "Please enter 0 or a positive integer for cutoff size");
                this.DialogResult = DialogResult.None;
            }
            else {
                this.errorProvider1.SetError(txtBoxBatchSize, "");
                
                this._taskHost.Properties[Constants.PERSIST_BATCHSIZE].SetValue(_taskHost, batchSize);
                this._taskHost.Properties[Constants.BULK_CUTOFF].SetValue(_taskHost, cutoff);
                this._taskHost.Properties[Constants.USE_BULK_API].SetValue(_taskHost, this.BulkApiBoolean.Checked);
                this._taskHost.Properties[Constants.PERSIST_USERNAME].SetValue(_taskHost, this.txtBoxUserName.Text.Trim());
                this._taskHost.Properties[Constants.PRESIST_PASSWORD].SetValue(_taskHost, this.mtxtBoxPassword.Text.Trim());
                this._taskHost.Properties[Constants.PERSIST_MAXTHREAD].SetValue(_taskHost, int.Parse(this.numericUpDown1.Value.ToString()));
                this._taskHost.Properties[Constants.PERSIST_NUMRETRIES].SetValue(_taskHost, int.Parse(this.numericUpDown2.Value.ToString()));
                this._taskHost.Properties[Constants.PERSIST_MANUALCOMPARISON].SetValue(_taskHost, this.chkBoxManualComparison.Checked);
                this._taskHost.Properties[Constants.PERSIST_SQLDESTCONMGR].SetValue(_taskHost, this.comboBox1.SelectedItem);
                this._taskHost.Properties[Constants.PERSIST_MESSAGELEVEL].SetValue(_taskHost, this.comboBox2.SelectedItem);
                this._taskHost.Properties[Constants.PERSIST_ALLOW_EXCEPTIONS].SetValue(_taskHost, allow);
                this._taskHost.Properties[Constants.PERSIST_SEND_DML_EMAIL].SetValue(_taskHost, sendDml);
            }

            if (allow)
            {

                this._taskHost.Properties[Constants.PERSIST_DEFAULT_STATUS].SetValue(_taskHost, this.DefaultStatusBox.SelectedIndex == 0);
                this._taskHost.Properties[Constants.PERSIST_EXCEPTION_TABLE_NAME].SetValue(_taskHost, this.tableNameBox.Text.Trim());
                this._taskHost.Properties[Constants.PERSIST_EXCEPTION_COLUMN_NAME].SetValue(_taskHost, this.ColumnNameBox.Text.Trim());
                this._taskHost.Properties[Constants.PERSIST_EXCEPTION_FORCE_BULK_COLUMN_NAME].SetValue(_taskHost, this.forceBulkBox.Text.Trim());
                this._taskHost.Properties[Constants.PERSIST_EXCEPTION_FORCE_STANDARD_COLUMN_NAME].SetValue(_taskHost, this.ForceStandardBox.Text.Trim());
                this._taskHost.Properties[Constants.PERSIST_EXCEPTION_OBJECT_COLUMN_NAME].SetValue(_taskHost, this.objectNameBox.Text.Trim());
            }

            if (sendDml)
            {
                this._taskHost.Properties[Constants.PERSIST_DML_TO_ADDRESS].SetValue(_taskHost, this.toTxtbox.Text.Trim());
                this._taskHost.Properties[Constants.PERSIST_DML_FROM_ADDRESS].SetValue(_taskHost, this.fromTxtbox.Text.Trim());
                this._taskHost.Properties[Constants.PERSIST_DML_SMTP_ADDRESS].SetValue(_taskHost, this.smtpTxtbox.Text.Trim());
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void txtBoxBatchSize_TextChanged(object sender, EventArgs e)
        {

        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void BulkApiBoolean_CheckedChanged(object sender, EventArgs e)
        {
            this.bulkCutoffBox.Enabled = this.BulkApiBoolean.Checked;
            
        }

        

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void AllowExceptions_CheckedChanged(object sender, EventArgs e)
        {

        }
    }
}